Searching in Linux
Finding Stuff in Linux :
locatewhereiswhichfind
locate:
- this command go through your entire file system and locate every occurence of the word wriiten
ex:
- sometimes the result of
locateis overwhelming or not practical - also locate command
Databasedoesn't update in current time it takes update once a day
whereis:
whereishelps to locate a binary file, this command returns not olny the location of the file /folder, it returns source and man-pageif they are avaliable.
ex:
which:
-
its more specific only returns the location of the binaries
PATHVariable. -
Note: PATH is an environment variable stores the location of the file/directory.
ex:
find: The Best search utility.
-
the
findcommand utility is the best of them all because it contains more detailed search features ex : it can give the permission, the owner, the group and lastley the date of a file while searching for it. -
findwalks through every folder and subfolder starting from the location you tell it (or your current folder if you don’t specify).
Tips
-
Use . to search from where you are.
-
Use /path/to/folder to search from a specific folder.
-
Use -name to search by file name.
-
Syntax
find </directory> -type <f/d/l> -name search_thing -
-type fsearching for a file -
-type dsearching for a Directory. -
type lsearch for symbolic link
Wildcards searching ex: *.md:
Search by Wildcard used in find and grep.
to understand about wild card with usages in linux we will create empty files ./home/kali/ hat bat cat and what
touch /home/kali/hat /home/kali/bat /home/kali/cat /home/kali/what
──(kali㉿kali)-[~]
└─$ find . -type f -name "[w]hat"
./what
┌──(kali㉿kali)-[~]
└─$ find . -type f -name "[b]at"
./bat
┌──(kali㉿kali)-[~]
└─$ find . -type f -name "[cb]at"
./bat
./cat
| Wildcard | Meaning | Example | Matches |
|---|---|---|---|
* |
Any number of characters | "*.txt" |
file.txt, abc.txt |
? |
Exactly one character | "b?t" |
bat, bit |
[c,b] |
One of the listed characters | "[cb]at" |
cat, bat |
grep: Searching in the file or piping:
grephelp us to search in files or config files and can be combined with other commands using piping|.| grepmost commonly used in piping because allowsgrepto grap the output of the previous command then can be used as an input.
Examples of piping with grep :
- see how the grep filtered the output of the
findcommand
Searching In files using grep example:
- as u can see the output gave us occurrence of the word
linuxin my.mdfile.